<?php

class PublicAction extends Action
{
    protected $Mod = array();
    protected $Config;
    protected $SysConfig;
    protected $cache_model;
    protected $Categorys;
    protected $Model;
    protected $modelid;
    protected $Urlrule;
    protected $Lang;

    function _initialize()
    {
        //检测是否登陆
        $this->checkLogin();
        $this->SysConfig = getSysConfig();
        $this->Model = getCache('Model');

        foreach ($this->Model as $key => $val) {
            $this->Mod[$val['tablename']] = $val['id'];
        }

        $this->Urlrule = getCache('Urlrule');
        $this->Lang = getCache('Lang');

        if (!empty($_GET['l'])) {
            if ($this->Lang[$_GET['l']]['id']) {
                $_SESSION['lang'] = $this->Lang[$_GET['l']];
            }
        } elseif (!$_SESSION['lang']) {
            $_SESSION['lang'] = $this->Lang[$this->SysConfig['DEFAULT_LANG']];
        }

        define('LANG_NAME', $_SESSION['lang']['mark']);
        define('LANG_ID', $_SESSION['lang']['id']);
        $this->assign('langid', LANG_ID);
        $this->Categorys = getCache('Category_' . LANG_NAME);
        $this->Config = getCache('Config_' . LANG_NAME);
        $this->assign('Lang', $this->Lang);
        $this->assign('model_name', MODULE_NAME);
        $this->assign('action_name', ACTION_NAME);

        // 后台用户权限检查
        $this->checkRbac();
        import("@.ORG.Form");
        $this->assign('form', new Form());

        $menu = D('AuthRule')->getAll();

        $current_menu = D('AuthRule')->getCurrentMenu(); //当前菜单
        if ($current_menu) {
            $parent_menu = D('AuthRule')->getParentMenu($current_menu); //获取面包屑导航
            $this->assign('parent_menu', $parent_menu);
        }
        $this->assign('current_menu', $current_menu); //当前菜单

        import('@.ORG.Tree');
        $tree = new Tree();
        if (empty($_SESSION[C('ADMIN_AUTH_KEY')])) {

            $rules = M('AuthGroup')->where('id=' . $_SESSION['role_id'])->getField('rules');
            $accessList = M('AuthRule')->where(array('id' => array('in', $rules)))->select();

            $all_menu_list = $tree->list_to_tree($accessList, 'id', 'pid'); //所有系统菜单
        } else {
            $all_menu_list = $tree->list_to_tree($menu, 'id', 'pid'); //所有系统菜单

        }

        $this->assign('all_menu_list', $all_menu_list); //所有菜单

        $this->assign('models', $this->Model);

    }

    public function cache()
    {
        dir_delete(RUNTIME_PATH . 'Cache/');
        dir_delete(RUNTIME_PATH . 'Data/');
        dir_delete(RUNTIME_PATH . 'Logs/');
        dir_delete(RUNTIME_PATH . 'Temp/');
        if (is_file(RUNTIME_PATH . '~runtime.php')) {
            @unlink(RUNTIME_PATH . '~runtime.php');
        }

        D('Category')->repair();

        $this->success('更新缓存成功！', U('Index/index'));
    }

    function checkLogin()
    {
        if (empty($_SESSION['admin']['username'])) {
            $this->redirect('Login/index');
        }
    }

    function checkRbac()
    {
        if (empty($_SESSION[C('ADMIN_AUTH_KEY')])) {
            import('ORG.Util.Auth');
            $auth = new Auth();
            $rule_name = GROUP_NAME . '/' . MODULE_NAME . '/' . ACTION_NAME;
            $result = $auth->check($rule_name, $_SESSION['adminid']);
            if (!$result) {
                $this->error('您没有权限访问');
            }
        }
    }
}